<?php

namespace App\Http\Controllers\Admin;

use App\Admin;
use App\Http\Response;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Hash;

class AuthController extends Controller
{
    use Response;
    public function login(Request $request)
    {
        $credentials = request(['username', 'password']);
        if (! $token = auth('admin')->attempt($credentials)) {
            return $this->error(505,'用户名或密码错误');
        }

        return $this->respondWithToken($token);
    }
    protected function respondWithToken($token)
    {
        return $this->success(200,[
            'api_token' => $token,
            'token_type' => 'bearer',
            'expires_in' => auth('api')->factory()->getTTL() * 360,
            'id'=>auth('admin')->user()->id,
            'uinfo'=>[
                'username'=>auth('admin')->user()->username,
                'status'=>auth('admin')->user()->status
            ]
        ]);
    }
    public function register(Request $request)
    {
        $credentials = request(['username', 'password']);
        $credentials['password']=Hash::make($credentials['password']);
        $credentials['status']=1;
        Admin::create($credentials);
    }
}
